package cs236369.hw5.db.beans;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import cs236369.hw5.db.DbBean;
import cs236369.hw5.db.types.DbMsgType;


@Entity
@Table(name="MESSAGES")
public class DbMessage implements DbBean
{
	@Id
	@Column(name = "ID", nullable = false, unique = false)
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="USER_ID", nullable = false, insertable = true, updatable = true)
	private DbUser user;

	@Column(name = "MSG_TYPE", unique = false, nullable = false)
	@Enumerated(EnumType.STRING)
	private DbMsgType type;
	
	public static final String X = "x"; 
	@Column(name = "X", nullable = false, unique = false)
	private Double x;
	
	public static final String Y = "y"; 
	@Column(name = "Y", nullable = false, unique = false)
	private Double y;

	
	public static final String DATE = "date"; //for hibernate criterias...
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "UPDATE_DATE", nullable = false)
	private Date date = new Date();

	@Column(name = "CONTENT", unique = false, nullable = false, length = 512)
	private String content;

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "msg", cascade=CascadeType.ALL )
	private List<DbEvent> events = new LinkedList<DbEvent>();

	public DbMessage(DbMsgType type, String content, Double x, Double y)
	{
		this.type = type;
		this.content = content;
		this.x = x;
		this.y = y;
	}
	
	protected DbMessage()
	{
		// empty ctor;
	}

	public Long getId()
	{
		return id;
	}

	public void setId(Long id)
	{
		this.id = id;
	}

	public DbUser getUser()
	{
		return user;
	}

	public void setUser(DbUser user)
	{
		this.user = user;
	}

	public DbMsgType getType()
	{
		return type;
	}

	public void setType(DbMsgType type)
	{
		this.type = type;
	}

	public Double getX()
	{
		return x;
	}

	public void setX(Double x)
	{
		this.x = x;
	}

	public Double getY()
    {
	    return y;
    }

	public void setY(Double y)
    {
	    this.y = y;
    }

	public Date getDate()
	{
		return date;
	}

	@PreUpdate
	@PrePersist
	protected void onDate()
	{
		this.date = new Date();
	}

	public void setDate(Date date)
	{
		this.date = date;
	}

	public String getContent()
	{
		return content;
	}

	public void setContent(String content)
	{
		this.content = content;
	}

	/**
	 * if you choose to add an event throw this get method, <br>
	 * don't forget to set the event's msg
	 * @return the list of all the events...
	 */
	public List<DbEvent> getEvents()
    {
	    return events;
    }
	
	public void addEvent(DbEvent e)
	{
		e.setMsg(this);
		events.add(e);
	}

	public void setEvents(List<DbEvent> events)
    {
	    this.events = events;
    }

//	public DbEvent getEvent()
//	{
//		return event;
//	}
//
//	public void setEvent(DbEvent event)
//	{
//		this.event = event;
//	}
}
